package com.jy.api.service.impl;

import com.jy.api.dao.BillRuleDao;
import com.jy.api.domain.BillRule;
import com.jy.api.service.BillRuleService;
import com.jy.api.signUpReq.admin.BillRuleReq;
import com.jy.api.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by xiaohua on 2019/4/28.
 */
@Service
public class BillRuleServiceImpl implements BillRuleService {
    @Autowired
    private BillRuleDao billRuleDao;


    @Override
    public List<BillRule> findRuleList(String storeId) {
        return billRuleDao.findAllByStoreIdAndEnabled(storeId, true);
    }

    @Override
    public void deleteById(int id) {
        BillRule billRule = billRuleDao.findOne(id);

        if (null != billRule) {
            billRule.setEnabled(false);
        }
        billRuleDao.save(billRule);
    }

    @Override
    public Result updateOrSave(BillRuleReq req) {
        BillRule billRule;
        if (0 == req.getId()) {
            billRule = new BillRule();
            BeanUtils.copyProperties(req, billRule);
        } else {
            billRule=billRuleDao.findOne(req.getId());
            if (null!=billRule){
                BeanUtils.copyProperties(req,billRule);
            }
        }
        billRuleDao.save(billRule);

        return new Result(200,"操作成功");
    }


}
